package guoan.web.webCustomer;

import guoan.core.dto.DataGridModel;
import guoan.core.dto.PageResponse;
import guoan.core.dto.RestResponse;
import guoan.domain.Words;
import guoan.domain.WordsConfig;
import guoan.service.WordsConfigService;
import guoan.service.WordsService;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Preconditions;

@Controller
@RequestMapping("/webCustomer/words")
public class WordsController {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private WordsService wordsService;
	@Autowired
	private WordsConfigService wordsConfigService;

	/**
	 * 跳转到不同的内容页面
	 * 
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index() {
		return "/webCustomer/words/index";
	}

	@RequestMapping(value = "data", method = RequestMethod.POST)
	@ResponseBody
	public PageResponse<Words> data(DataGridModel dataGridModel) {
		return wordsService.page(dataGridModel);
	}

	/** 查看 */
	@RequestMapping(value = { "update", "/add" }, method = RequestMethod.GET)
	public String toUpdate(Long id, Model model) {
		if (id != null) {
			Words words = wordsService.findOne(id);
			model.addAttribute("words", words);
		}
		return "/webCustomer/words/add";
	}

	/** 查看 */
	@RequestMapping(value = { "reback" }, method = RequestMethod.GET)
	public String reback(Long id, Model model) {
		if (id != null) {
			Words words = wordsService.findOne(id);
			model.addAttribute("reback", words.getReback());
		}
		return "/webCustomer/words/reback";
	}

	/** 保存 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public RestResponse<Boolean> save(Words words, HttpServletRequest request) {
		Preconditions.checkNotNull(words, "参数错误");
		wordsService.save(words);
		return RestResponse.ok(true);
	}

	@RequestMapping(value = "del", method = RequestMethod.GET)
	@ResponseBody
	public RestResponse<Boolean> del(Long id) {
		Preconditions.checkNotNull(id, "参数错误");
		wordsService.del(id);
		return RestResponse.ok(true);
	}

	/** 查看 */
	@RequestMapping(value = { "config/update", "config/add" }, method = RequestMethod.GET)
	public String toUpdate(Model model) {
		WordsConfig wordsConfig = wordsConfigService.initOne();
		model.addAttribute("wordsConfig", wordsConfig);
		return "/webCustomer/words/config/add";
	}

	/** 查看 */
	@RequestMapping(value = "config/save", method = RequestMethod.POST)
	@ResponseBody
	public RestResponse<Boolean> save(WordsConfig wordsConfig, HttpServletRequest request) {
		wordsConfigService.save(wordsConfig);
		return RestResponse.ok(true);
	}
}
